package cn.com.bluemoon.daps.sync.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.enums.SyncStatus;
import cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask;
import cn.com.bluemoon.daps.sync.feignclient.DapModelModuleFacade.ModelInfo;
import cn.com.bluemoon.daps.sync.service.DapSyncManagerService;
import cn.com.bluemoon.daps.sync.vo.syncmanage.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 针对发布同步中失败的任务+表进行管理-修改状态、人工修改表结构
 *
 * @author Jarod.Kong
 * @date 2021/6/16 16:57
 */
@Api(tags = "同步管理")
@RestController
@Slf4j
@Validated
@RequestMapping("/management")
public class DapSyncManagerController {
    @Resource
    private DapSyncManagerService syncManagerService;

    @BmPermission(value = {"model:sync:management"}, name = "同步管理模型列表")
    @GetMapping("/modelInfo")
    @ApiOperation(value = "同步管理-模型列表")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = ModelInfo.class)})
    public ResultBean<List<ModelInfo>> modelInfo() {
        List<ModelInfo> modelInfos = syncManagerService.modelInfo();
        return ResultBean.ok(modelInfos);
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理列表")
    @PostMapping
    @ApiOperation(value = "同步管理-列表")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = SyncManageOut.class)})
    public ResultBean<IPage<SyncManageOut>> managements(@RequestBody @ApiParam(value = "同步管理分页查询条件") @Valid SyncManagementPageVo<DapSyncProdEnvTask> searchVo) {
        IPage<SyncManageOut> res = syncManagerService.querySyncList(searchVo);
        return ResultBean.page(res);
    }

    @BmPermission(value = {"model:sync:management"}, name = "查看同步详情")
    @PostMapping("/info")
    @ApiOperation(value = "同步管理-同步详情-列表")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = SyncEnvInfo.class)})
    public ResultBean<List<SyncEnvInfo>> viewSyncEnvs(@Valid @RequestBody SyncTableParam param) {
        List<SyncEnvInfo> res = syncManagerService.viewSyncEnvs(param);
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理-同步详情-列表-日志")
    @PostMapping("/info/log")
    @ApiOperation(value = "同步管理-同步详情-列表-日志")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResultBean<String> viewLogInfo(@Valid @RequestBody SyncTableEnvParam param) {
        String res = syncManagerService.viewLogInfo(param);
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理-同步详情-列表-日志")
    @PostMapping("/info/confirmSync")
    @ApiOperation(value = "同步管理-同步详情-列表-确认同步")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", response = SyncConfirmOut.class)})
    public ResultBean<SyncConfirmOut> confirmSync(@Valid @RequestBody SyncTableEnvParam param) {
        SyncConfirmOut res = syncManagerService.confirmSync(param);
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理修改状态")
    @PostMapping("/info/confirmSync/preStatus")
    @ApiOperation(value = "同步管理-同步详情-列表-确认同步-修改状态（判断是否都是可强制执行语句）")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResultBean<Boolean> allMatchForceUpdateStatusPredicate(@Valid @RequestBody SyncTableEnvAndRealDdlParam param) {
        boolean res = syncManagerService.allMatchForceUpdateStatusPredicate(param, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理修改状态")
    @PostMapping("/info/confirmSync/status")
    @ApiOperation(value = "同步管理-同步详情-列表-确认同步-修改状态（模型最新版本覆盖数据库结构）")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = String.class),
            @ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResultBean<String> updateStatus(@Valid @RequestBody SyncTableEnvAndRealDdlParam param) {
        ResultBean<String> res = syncManagerService.syncStatus(param, UserInfoHolder.getUserInfoOrEmpty(), SyncStatus.SYNC_DONE);
        return res;
    }

    @BmPermission(value = {"model:sync:management"}, name = "同步管理修改状态")
    @PostMapping("/info/confirmSync/repairStatus")
    @ApiOperation(value = "同步管理-同步详情-列表-确认同步-修改状态（数据库结构覆盖模型最新版本）")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = String.class),
            @ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResultBean<String> updateRepairStatus(@Valid @RequestBody SyncTableEnvAndRealDdlParam param) {
        ResultBean<String> res = syncManagerService.syncRepairStatus(param, UserInfoHolder.getUserInfoOrEmpty(), SyncStatus.SYNC_DONE);
        return res;
    }
}
